
using MediatR;
using MediatR_.MediatR_Pipeline;
using Serilog;
using System.Reflection;
using System.Runtime.Loader;
using 日志的使用.自定义的内容;
using 日志的使用.自定义的内容.托管服务;

namespace 日志的使用
{
    public class Program
    {
        public static void Main(string[] args)
        {
            //一，构造了一个Web的Builder -建造者模式
            WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

            // Add services to the container.

            builder.Services.AddControllers();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen();

            #region 读取配置信息
            //读取电脑环境变量的值
            var values = Environment.GetEnvironmentVariable("DefaultDB_ConnStr", EnvironmentVariableTarget.Machine);
            //使用的GetSection("ConnectionStrings")
            var mysqlConStr = builder.Configuration.GetConnectionString("MySqlConStr");
            mysqlConStr = builder.Configuration.GetSection("Logging:LogLevel:Default").Get<string>();
            //读取私密文件中的内容
            var secretStr = builder.Configuration.GetSection("secret").Get<string>();
            #endregion

            #region 注册服务Service
            //builder.Services.AddSingleton<IServiceCollection, ServiceCollection>();
            //builder.Services.AddScoped<IServiceCollection, ServiceCollection>();
            //builder.Services.AddTransient<IServiceCollection, ServiceCollection>(); 
            builder.Services.AddSingleton<SelfMiddleUseInterface>();
            #endregion


            //通过Dll名称，扫描相应的实例，添加到容器中
            // builder.AddServices("MediatR_",ServiceLifetime.Scoped);
            //builder.Services.AddMediatR(cfg => {
            //    cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly());
            //});
            //使用MediatR
            builder.Services.AddMediatR(AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName("MediatR_")));
            builder.Services.AddScoped(typeof(IPipelineBehavior<,>), typeof(MyPipelingBehavior<,>));


            //添加托管服务
            builder.Services.AddHostedService<SelfHostedService>();
            builder.Services.AddHostedService<SelfBackgroundService>();

            //添加Serilog到容器中,自定义的扩展方法
            builder.AddSerilog();
            //添加Cors到窗口中,跨域访问
            builder.AddUseCors("http://localhost:3000");
            //添加服务器缓存
            builder.Services.AddMemoryCache();
            //添加Redis的使用
            builder.AddRedis();
            //添加Filter的使用
            builder.AddFilter();

            //二、 构造一个Web应用程序
            WebApplication app = builder.Build();

            //添加中间件
            app.UseMiddleware<SelfMiddleware>("Files");
            app.UseMiddleware<SelfMiddleUseInterface>();

            //请求级别的处理
            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }

            //使用CORS跨域
            app.UseCors();
            app.UseAuthorization();
            app.MapControllers();
            //三、运行web应用程序
            app.Run();
        }
    }
}